//
//  YHYHunterCityViewController.m
//  TripTogether
//
//  Created by 杨泓晔 on 15/12/22.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "YHYHunterCityViewController.h"
#import "YHYHunterCityTableViewCell.h"
#import "YHYHunterCityModel.h"
#import "YHYHunterCityDataViewController.h"
#import "YHYLeftBarViewController.h"
@interface YHYHunterCityViewController ()<UITableViewDataSource,UITableViewDelegate,YHYLeftBarViewControllerDelegate>
@property (nonatomic,strong)UITableView *tableView ;
@property (nonatomic, strong)MBProgressHUD *HUB;
@property (nonatomic,strong)UIButton *myLeftButton;
@property (nonatomic,strong)NSMutableArray *hunterArray;
@property (nonatomic,assign)BOOL upload;
@property (nonatomic,assign)NSInteger pageA;

@end

@implementation YHYHunterCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _hunterArray = [NSMutableArray array];

    self.view.backgroundColor = [UIColor colorWithRed:0.980 green:0.961 blue:0.910 alpha:1.000];
    
    _myLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _myLeftButton.frame = CGRectMake(0, 0, 100, 20);
    _myLeftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
   
    [_myLeftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside ];
//    _myLeftButton.backgroundColor = [UIColor orangeColor];
    _myLeftButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [_myLeftButton setTitle:@"北京" forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_myLeftButton];
    self.navigationItem.title = @"走走带你到处转!";

    
    [self createTableView];
    
//    //集成
//    self.upload = NO;
//#pragma mark *** 第二步：调用上拉下拉方法
//    [self addHeader];
//    [self addFooter];
//
    

    
   
    
}

//下拉刷新
- (void)addHeader{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.upload = NO;
        self.pageA = 0;
        
        
          }];
      // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    


}
- (void)addFooter{
    
}

- (void)loadNewData:(MJRefreshNormalHeader*)loda{
    
}

- (void)createTableView{
    _tableView = [UITableView new];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.rowHeight = 370;


    [_tableView registerClass:[YHYHunterCityTableViewCell class] forCellReuseIdentifier:@"cell"];
    
//tableView 位置大小
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.view);
        make.top.and.left.mas_equalTo(self.view);
        
    }];
    
    
    [self getDataName:@"北京"];
   
}

- (void)leftAction:(UIButton *)bar{
    YHYLeftBarViewController * VC = [YHYLeftBarViewController new];
    VC.myDelegate = self;

    [self.navigationController presentViewController:VC animated:YES completion:nil];
}
- (void)presentDelegateName:(NSString *)cityName{
    [_myLeftButton setTitle:cityName forState:UIControlStateNormal];
    [self getDataName:cityName];
    
}
#pragma mark - getData
- (void)getDataName:(NSString *)cityName{
    
    
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUB show:YES];
    self.HUB.labelText = @"正在加载..";
    
    
    
    // NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/hunter/products/more/?start=0&city_name=%@&lat=38.882332558663585&lng=121.53934415009309",cityName];
    
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/hunter/products/more/?start=0&city_name=%@",cityName];
    NSLog(@"%@",cityName);
    [WJQNetwork getAFNet:url block:^(id result) {
        
        [_hunterArray removeAllObjects];
        
        
        
        NSMutableArray *listArray = [result objectForKey:@"product_list"];
        
        for (NSMutableDictionary *dic in listArray) {
            
            YHYHunterCityModel *hunterModel = [YHYHunterCityModel modelWithDicationary:dic];
            

            [_hunterArray addObject:hunterModel];
        }
        
        
        [_tableView reloadData];
        
        [self.HUB hide:YES];
        
    } errorBlock:^(NSError *error) {
        
        [self NetworkError:^{
            
        }];
    }];
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _hunterArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellHunterCity = @"cell";

    YHYHunterCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellHunterCity];
         //model 赋值传值
    YHYHunterCityModel *model = _hunterArray[indexPath.row];
    cell.myYHYHunterCityModel = model;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    YHYHunterCityModel *hunterModel = [[YHYHunterCityModel alloc] init];
    hunterModel = [self.hunterArray objectAtIndex:indexPath.row];
    
    YHYHunterCityDataViewController *YHYVC = [YHYHunterCityDataViewController new];
    YHYVC.product_id = hunterModel.product_id;
    
    [self.navigationController pushViewController:YHYVC animated:YES];
    
    
    
   }




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
